package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.fragment.yushou.AllYuShouFragment;
import com.wnhz.work.yz.fragment.yushou.AuditYuShouFragment;
import com.wnhz.work.yz.fragment.yushou.ProceedYuShouFragment;

import java.util.ArrayList;
import java.util.List;

public class AllYuShouActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private AllYuShouFragment fragment1;
    private AuditYuShouFragment fragment2;
    private ProceedYuShouFragment fragment3;

    private List<Fragment> fragmentList;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    private TextView text1, text2, text3;
    private View line1, line2, line3;

    private int textColorNor, textColorCli;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_yu_shou);
        initData();
        initView();
    }

    private void initData() {
        textColorNor = getResources().getColor(R.color.text_two);
        textColorCli = getResources().getColor(R.color.main_color);
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("预售列表");
        findViewById(R.id.btn_main_left).setOnClickListener(this);

        text1 = (TextView) findViewById(R.id.tv_all);
        text2 = (TextView) findViewById(R.id.tv_audit);
        text3 = (TextView) findViewById(R.id.tv_proceed);

        line1 = findViewById(R.id.line1);
        line2 = findViewById(R.id.line2);
        line3 = findViewById(R.id.line3);

        text1.setOnClickListener(this);
        text2.setOnClickListener(this);
        text3.setOnClickListener(this);

        fragmentList = new ArrayList<>();
        fragment1 = new AllYuShouFragment();
        fragment1.setActivity(this);
        fragment2 = new AuditYuShouFragment();
        fragment2.setActivity(this);
        fragment3 = new ProceedYuShouFragment();
        fragment3.setActivity(this);
        fragmentList.add(fragment1);
        fragmentList.add(fragment2);
        fragmentList.add(fragment3);

        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.fl_project, fragment1);
        transaction.add(R.id.fl_project, fragment2);
        transaction.add(R.id.fl_project, fragment3);

        transaction.hide(fragment2);
        transaction.hide(fragment3);
        transaction.commit();
        // showDialog();
        //  upload(type);

    }

    private void hideFragment(int index) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        for (int i = 0; i < fragmentList.size(); i++) {
            if (i == index) {
                transaction.show(fragmentList.get(i));
            } else {
                transaction.hide(fragmentList.get(i));
            }
        }
        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //全部
            case R.id.tv_all:
                hideFragment(0);
                transaction.show(fragmentList.get(0)).commit();
                text1.setTextColor(textColorCli);
                text2.setTextColor(textColorNor);
                text3.setTextColor(textColorNor);

                line1.setVisibility(View.VISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.INVISIBLE);

                //    upload("0");
                break;

            //审核中
            case R.id.tv_audit:
                hideFragment(1);
                transaction.show(fragmentList.get(1)).commit();
                text1.setTextColor(textColorNor);
                text2.setTextColor(textColorCli);
                text3.setTextColor(textColorNor);

                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.VISIBLE);
                line3.setVisibility(View.INVISIBLE);

                //    upload("1");
                break;

            //进行中
            case R.id.tv_proceed:
                hideFragment(2);
                transaction.show(fragmentList.get(2)).commit();
                text1.setTextColor(textColorNor);
                text2.setTextColor(textColorNor);
                text3.setTextColor(textColorCli);

                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.VISIBLE);
                //    upload("2");
                break;
            default:
                break;
        }
    }
}
